//
//  MainViewController.swift
//  swiftWeibo
//
//  Created by WangQiang on 2016/10/17.
//  Copyright © 2016年 WangQiang. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // iOS7以后只需要设置tintColor, 那么图片和文字都会按照tintColor渲染
        tabBar.tintColor = .orange //渲染颜色
        
        addChildViewControllers()
      
        
    }
    
    func addChildViewControllers() {
        
        guard let filePath = Bundle.main.url(forAuxiliaryExecutable: "MainVCSettings.json")  else {
            NJLog("文件不存在")
            return
        }
        
        guard let data =  NSData.init(contentsOf: filePath) else{
            
            return
        }
        
        do {
            //data(withJSONObject: data as Data, options:JSONSerialization.ReadingOptions.mutableContainers) as! [[String:AnyObject]]
            let objc = try JSONSerialization.jsonObject(with: data as Data, options:.mutableContainers) as! [[String:AnyObject]]
            
            for dict in objc {
                let title = dict["title"] as? String
                let vcName = dict["vcName"] as? String
                let imageName = dict["imageName"] as? String
                addChildViewController(vcName, title: title, imageName: imageName)
            }
        } catch{
            // 只要try对应的方法发生了异常, 就会执行catch{}中的代码
            addChildViewController("HomeTableViewController", title: "首页", imageName: "tabbar_home")
            addChildViewController("MessageTableViewController", title: "消息", imageName: "tabbar_message_center")
            addChildViewController("NullViewController", title: "", imageName: "")
            addChildViewController("DiscoverTableViewController", title: "发现", imageName: "tabbar_discover")
            addChildViewController("ProfileTableViewController", title: "我", imageName: "tabbar_profile")
        }

        
//        addChildViewController(childViewController: HomeTableViewController(), title: "首页", imageName: "tabbar_home")
//        addChildViewController(childViewController: MessagesTableViewController(), title: "消息", imageName: "tabbar_message_center")
//        addChildViewController(childViewController: DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
//        addChildViewController(childViewController: ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        tabBar.addSubview(composeBtn)
        let rect  = composeBtn.frame
        let width = tabBar.bounds.width / CGFloat(childViewControllers.count)
        composeBtn.frame = .init(x: 2*width, y: 0, width: width, height: rect.height)
    }
    func addChildViewController(_ childViewControllerName:String?,title:String?,imageName:String?) {
        guard let nameSpace = Bundle.main.infoDictionary![kCFBundleExecutableKey as String]   as? String else{
            NJLog("获取命名空间失败")
            return
        }
        var cls:AnyClass?
        if let vcName = childViewControllerName {
            cls = NSClassFromString(nameSpace + "." + vcName)
        }
        
        guard let typeCls = cls as? UITableViewController.Type else {
            NJLog("\(cls)不是TableViewController类型")
            return
        }
        
        let childViewController = typeCls.init()
        childViewController.title = title
        if let ivName = imageName{
            childViewController.tabBarItem.image = UIImage.init(named: ivName)
            childViewController.tabBarItem.selectedImage = UIImage.init(named: ivName+"_highlighted")
        }
        let nav = UINavigationController.init(rootViewController: childViewController)
        addChildViewController(nav)
    }
//    func addChildViewController(childViewController:UIViewController ,title:String,imageName:String) {
//        
//        childViewController.tabBarItem.title = title;
//        childViewController.tabBarItem.image = UIImage(named:imageName)
//        childViewController.tabBarItem.selectedImage = UIImage(named:imageName+"_highlighted")
//        let nav = UINavigationController(rootViewController:childViewController)
//        addChildViewController(nav)
//    
//    }
    
    /*
     public : 最大权限, 可以在当前framework和其他framework中访问
     internal : 默认的权限, 可以在当前framework中随意访问
     private : 私有权限, 只能在当前文件中访问
     以上权限可以修饰属性/方法/类
     
     在企业开发中建议严格的控制权限, 不想让别人访问的东西一定要private
     */
    // 如果给按钮的监听方法加上private就会报错, 报错原因是因为监听事件是由运行循环触发的, 而如果该方法是私有的只能在当前类中访问
    // 而相同的情况在OC中是没有问题, 因为OC是动态派发的
    // 而Swift不一样, Swift中所有的东西都在是编译时确定的
    // 如果想让Swift中的方法也支持动态派发, 可以在方法前面加上 @objc
    // 加上 @objc就代表告诉系统需要动态派发
    @objc private func composeBtnClick(sender:UIButton){
        
    }
    
    private lazy var composeBtn:UIButton = {
        ()-> UIButton in
        let btn = UIButton()
    btn.setImage(UIImage.init(named:"tabbar_compose_icon_add"), for:.normal)
    btn.setImage(UIImage.init(named:"tabbar_compose_icon_add_highlighted"), for: .highlighted)
    
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), for:.normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), for:.highlighted)
       
        btn.addTarget(self, action: Selector(("composeBtnClick:")), for:.touchUpInside)
         btn.sizeToFit()
        return btn
    }()
}
